<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <h1>智慧园区-登录</h1>
      <el-card shadow="never" class="login-card">
        <el-form ref="form" :model="formData" :rules="rules">

          <el-form-item label="账号" label-position="top" prop="username">
            <el-input v-model="formData.username" />
          </el-form-item>

          <el-form-item label="密码" label-position="top" prop="password">
            <el-input v-model="formData.password" type="password" show-password />
          </el-form-item>

          <el-form-item prop="remember">
            <el-checkbox v-model="formData.remember">记住我</el-checkbox>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" class="login_btn" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<script>
const FORMDATA_KEY = 'form_key'
export default {
  name: 'Login',
  data() {
    return {
      formData: {
        username: 'demo',
        password: 'Hmzs%001',
        remember: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    const cacheFormStr = localStorage.getItem(FORMDATA_KEY)
    if (cacheFormStr) {
      const cacheFormData = JSON.parse(cacheFormStr)
      this.formData.username = cacheFormData.username
      this.formData.password = cacheFormData.password
    }
  },
  methods: {
    async login() {
      await this.$refs.form.validate()
      const { username, password, remember } = this.formData
      console.log('index222', username)
      await this.$store.dispatch('user/doLogin', { username, password })
      console.log('index111', { username, password })

      if (remember) {
        localStorage.setItem(FORMDATA_KEY, JSON.stringify({ username, password }))
      } else {
        localStorage.removeItem(FORMDATA_KEY)
      }
      console.log('登录成功')
      this.$router.push('/')
    }
  }
}
</script>
<style lang="scss">
.login-container {
  display: flex;
  align-items: stretch;
  height: 100vh;

  .logo {
    flex: 3;
    background: rgba(38, 72, 176) url(../../assets/login-bg.svg) no-repeat center / cover;
    border-top-right-radius: 60px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    padding: 0 100px;

    .icon {
      background: url(../../assets/hmzs-logo.png) no-repeat 70px center / contain;
      width: 300px;
      height: 50px;
      margin-bottom: 50px;
    }

    p {
      color: #fff;
      font-size: 18px;
      margin-top: 20px;
      width: 300px;
      text-align: center;
    }
  }

  .form {

    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 176px;

    .el-card {
      width: 350px;
      // background-color: red;
      border: none;
      padding: 0;
    }

    .login_btn {
      width: 310px;
      height: 44px;
    }

    h1 {
      padding-left: 90px;
      font-size: 24px;
    }

    .el-input {
      // width: 350px;
      // height: 44px;

      .el-input__inner {
        background: #f4f5fb;
      }
    }

    .el-checkbox {
      color: #606266;
    }
  }
}
</style>
